# coding: utf8
# models/plugin_plugin.py
## This file was created using static/plugin_plugin/model_template using the plugin_plugin
# copyright Christopher Steel, 2011, Voice of Access
#########################################
# initialization.py
import os
cwd = os.getcwd()
current_app = os.path.join(cwd,'applications',request.application)
initialization_file_path = os.path.join(current_app,'models','000_uc_initialization.py')

############################
# set plugin_uc_jump global
# 
plugin_uc_jump_installed = True
plugin_uc_jump_menu_enabled = True
plugin_uc_jump_menu_options_menu_enabled = False
plugin_uc_jump_menu_user_menu_enabled = False
plugin_uc_jump_menu_auth_menu_enabled = False

###################################
# initialization file related code
###################################
#
########################################
# ensure we have an initialization file
#
def create_initialization_file():
    if not os.path.exists(initialization_file_path):
        our_file = open(initialization_file_path,"w")
        our_text = '# file created by plugin_uc_jump\n'
        print('created %s' % initialization_file_path)
    else:
        if 'plugin_debug_enabled' in globals():
            if plugin_debug_enabled == True:
                print('found %s' % initialization_file_path)

####################################
# append to our initialization file
#
def append_initialization_file(append):
    our_file = open(initialization_file_path,"w")
    our_file.writelines(append)
    our_file.close()
    if 'plugin_debug_enabled' in globals():
        if plugin_debug_enabled == True:
            print('appended %s to %s' % (append,initialization_file_path))

#######################################
# if no initialization file create one
#
if not 'initialization_file_created' in globals():
    create_initialization_file()
    append = '# entry created by plugin_uc_jump\n'
    append_initialization_file(append)
    append = "initialization_file_created = True\n"
    append_initialization_file(append)
else:
    if 'plugin_debug_enabled' in globals():
        if plugin_debug_enabled == True:
            print("found 'initialization_file_created' in globals")

####################################
# ensure group_plugin_uc_jump_admin 
#
if not 'group_plugin_uc_jump_admin_created' in globals():
    if not db(db.auth_group.role=='plugin_uc_jump_admin').select():
        db.auth_group.insert(role='plugin_uc_jump_admin',description='user to administer plugin_uc_jump')
    else:
        if 'plugin_debug_enabled' in globals():
            if plugin_debug_enabled == True:
                print("found db.auth_group.role=='admin_plugin_plugin' in database")
    append_initialization_file('# entry created by plugin_uc_jump\n')
    append_initialization_file('group_plugin_uc_jump_admin_created = True\n')
else:
    if 'plugin_debug_enabled' in globals():
        if plugin_debug_enabled == True:
            print('found "group_plugin_uc_jump_admin_created" in globals')

#####################################
# ensure group_plugin_uc_jump_config
#
if not 'group_plugin_uc_jump_config_created' in globals():
    if not db(db.auth_group.role=='plugin_uc_jump_config').select(): 
        db.auth_group.insert(role='plugin_uc_jump_config',description='user to configure plugin_uc_jump')
        append = '# entry created by plugin_uc_jump\n'
        append_initialization_file(append)
        append = 'group_plugin_uc_jump_config_created = True\n'
        append_initialization_file(append)
    else:
        if 'plugin_debug_enabled' in globals():
            if plugin_debug_enabled == True:
                print("found db.auth_group.role=='config_plugin_plugin' in database")
else:
    if 'plugin_debug_enabled' in globals():
        if plugin_debug_enabled == True:
            print('found "group_plugin_uc_jump_config_created" in globals')

####################################
# ensure 'plugin_uc_jump_installed'
#
if not 'plugin_uc_jump_installed' in globals():
    our_file = open(initialization_file_path,"a")
    append = '# entry created by plugin_uc_jump\n'
    append_initialization_file(append)
    append = 'plugin_uc_jump_installed = True\n'
    append_initialization_file(append)
else:
    if 'plugin_debug_enabled' in globals():
        if plugin_debug_enabled == True:
            print('found "models/plugin_uc_jump/initialization.py" in globals')
            print('found "plugin_uc_jump_installed" in globals')

###############################
# response.menu_plugin_uc_jump
#
if 'plugin_uc_jump_menu_enabled' in globals() and plugin_uc_jump_menu_enabled == True:
    '''
    if response_menu_jump_enabled then we create and display it. other wise skip it.
    '''   
    response.menu_jump = []
    response.menu_jump += [
        (T('Home'), False, URL('default','index#content'), [])
        ]

    if 'plugin_uc_janrain_installed' in globals() and plugin_uc_janrain_installed == True:
        response.menu_jump += [
            (T('User Menu'), False, URL(request.application,'plugin_uc_janrain','user/login#content'), [])
            ]
    else:
        response.menu_jump += [
            (T('User Menu'), False, URL('default','user#content'), [])
            ]

    #response.menu_jump += [
    #    (T('Accessibility'), False, URL('plugin_uc_jump','accessibility#content'), [])
    #    ]
    
    response.menu_jump_return = [
        (T('Return to jump menu'), False, URL('default','index#jump-menu'), [])
        ]    

    if 'plugin_uc_jump_menu_options_menu_enabled' in globals() and plugin_uc_jump_menu_options_menu_enabled == True:
        response.menu+= [
            (T('plugin_uc_jump'), False, URL('plugin_uc_jump','index'),
                [
                    (T('plugin options'), False, URL('plugin_uc_jump','index'),
                        [
                            (T('option_1'), False, URL('plugin_uc_jump', 'option_1')),
                        ]),
                    (T('about'), False, URL(request.application,'plugin_uc_jump','display_static',args='about')),
                    (T('contribute'), False, URL(request.application,'plugin_uc_jump','display_static',args='contribute')),  
                    (T('license'), False, URL(request.application,'plugin_uc_jump','display_static',args='license')),
                    (T('universal cake'), False, URL(request.application,'plugin_uc_jump','display_static',args='universal_cake')),
                    (T('voice of access'), False, URL(request.application,'plugin_uc_jump','display_static',args='voice_of_access')),
                ]
           )]

    ##############################################
    # site plugin_plugin users menu
    #
    # plugin_uc_jump menu is displayed to users who are signed in
    
    if auth.is_logged_in():
        if 'plugin_uc_jump_menu_user_menu_enabled' in globals() and plugin_uc_jump_menu_user_menu_enabled == True:
            response.menu+= [    
            (T('plugin_uc_jump user menu'), False, URL('plugin_uc_jump','index'),
                [
                    (T('plugin_uc_jump user actions'), False, URL('plugin_uc_jump','index'),
                        [
                            (T('configure plugin_uc_jump'), False, URL('plugin_uc_jump', 'configure')),
                        ]),          
                ]
           )]    

    ##############################################
    # site plugin_plugin administrators menu
    #
    # plugin_uc_jump menu is displayed to registered signed in users who
    # are also members of the admin_plugin_plugin group.
    if auth.is_logged_in() and auth.has_membership(auth.id_group('admin_plugin_uc_jump')):
        if 'plugin_uc_jump_menu_admin_menu_enabled' in globals() and plugin_uc_jump_menu_admin_menu_enabled == True:
            response.menu+= [    
                ('plugin_uc_jump admin', False, URL('plugin_uc_jump','admin'),
               )]
